package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.TopicParam;
import com.cskaoyan.bean.bo.WxBaseParam;
import com.cskaoyan.bean.pojo.MallTopic;
import com.cskaoyan.bean.pojo.MallTopicExample;
import com.cskaoyan.bean.vo.RelatedTopicVO;
import com.cskaoyan.bean.vo.TopicListVO;
import com.cskaoyan.bean.vo.WxTopicDetailVO;
import com.cskaoyan.mapper.MallTopicMapper;
import com.cskaoyan.service.TopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author JJ
 * @description TopicServiceImpl专题接口的实现类
 * @time 2021/7/9 17:43
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    MallTopicMapper mallTopicMapper;

    @Override
    public BaseData<MallTopic> listTopic(TopicParam param) {
        //执行查询之前写一行代码即可
        PageHelper.startPage(param.getPage(), param.getLimit());

        MallTopicExample mallTopicExample = new MallTopicExample();

        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();
        //查询需要设置一个排序和是否删除
        criteria.andDeletedEqualTo(false);
        if (param.getTitle() != null) {
            criteria.andTitleLike("%" +param.getTitle()+"%");
        }
        if (param.getSubtitle() != null) {
            criteria.andSubtitleLike("%" +param.getSubtitle()+"%");
        }

        mallTopicExample.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MallTopic> mallTopicList = mallTopicMapper.selectByExampleWithBLOBs(mallTopicExample);

        //total可以使用countByExample，也可以使用PageHelper来获得
        //在执行查询之后可以获得分页信息,将查询结果放入PageInfo的构造方法里
        PageInfo<MallTopic> pageInfo = new PageInfo<>(mallTopicList);
        int total = (int) pageInfo.getTotal();

        return BaseData.ok(mallTopicList, total);

    }


    @Override
    public Integer createTopic(MallTopic param) {


        Date date = new Date();
        param.setAddTime(date);
        param.setUpdateTime(date);

        //插入创建时间及更新时间后添加
        int i = mallTopicMapper.insertSelective(param);
        return i;
    }


    @Override
    public List<MallTopic> queryTopic(MallTopic param) {
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();

        if (param.getId()!=null){
            criteria.andIdEqualTo(param.getId());
        }
        if (param.getTitle()!=null){
            criteria.andTitleEqualTo(param.getTitle());
        }
        if (param.getSubtitle()!=null){
            criteria.andSubtitleEqualTo(param.getSubtitle());
        }
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);

        return mallTopics;

    }


    @Override
    public Integer updateTopic(MallTopic param) {
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();

        if (param.getId() != null) {
            criteria.andIdEqualTo(param.getId());
        }

        //由于前端代码bug,更新的时候不能采用它的addtime
        param.setAddTime(null);
        //插入更新时间
        Date date = new Date();
        param.setUpdateTime(date);

        int i = mallTopicMapper.updateByExampleSelective(param, mallTopicExample);
        return i;
    }


    @Override
    public Integer deleteTopic(MallTopic param) {
        MallTopicExample mallTopicExample = new MallTopicExample();


        MallTopicExample.Criteria criteria = mallTopicExample.createCriteria();

        criteria.andIdEqualTo(param.getId());
        //逻辑删除
        param.setDeleted(true);

        int i = mallTopicMapper.updateByExampleSelective(param, mallTopicExample);

        return i;
    }

    @Override
    public WxTopicDetailVO queryMallTopicById(Integer id) {
        MallTopic mallTopic = mallTopicMapper.selectByPrimaryKey(id);
        WxTopicDetailVO topicDetailVO = new WxTopicDetailVO(mallTopic, mallTopic.getGoods());
        return topicDetailVO;
    }

    @Override
    public TopicListVO queryTopicListByPageAndSize(WxBaseParam param) {
        PageHelper.startPage(param.getPage(),param.getSize());

        MallTopicExample example = new MallTopicExample();
        MallTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(example);
        long count = mallTopicMapper.countByExample(example);
        TopicListVO listVO = new TopicListVO(mallTopics, (int) count);

        return listVO;
    }

    // 查出前4条，将其作为推荐使用，如果推荐的在前四条，那么顺延下一条
    @Override
    public RelatedTopicVO getRelatedTopic(Integer id) {
        List<MallTopic> mallTopics = mallTopicMapper.selectByConditions(id);
        return new RelatedTopicVO(mallTopics);
    }


}
